package com.hb.controller.role;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.hb.model.Result;
import com.hb.pojo.dto.role.RoleDto;
import com.hb.pojo.po.role.Role;
import com.hb.pojo.vo.role.RoleVo;
import com.hb.pojo.vo.validated.GroupA;
import com.hb.pojo.vo.validated.GroupB;
import com.hb.pojo.vo.validated.GroupD;
import com.hb.service.role.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "role Related interfaces")
@Transactional
@Validated
@RestController
@RequestMapping("roles")
public class RoleController {

	@Resource
	private RoleService roleService;

	/**
	 * Add a role
	 */
	@ApiIgnore
	@ApiOperation(value = "Add a role", notes = "Add a role")
	@PostMapping()
	public Result<Role> insertSelectiveOne(@RequestBody @Parameter(name = "add role") @Validated(GroupA.class) RoleVo roleVo) {
		Role role = roleService.insertSelective(roleVo);
		return role != null ? Result.success(role) : Result.error();
	}

	/**
	 * Update a role
	 */
	@ApiIgnore
	@ApiOperation(value = "update a role", notes = "update a role")
	@PatchMapping()
	public Result<Role> updateSelectiveOne(@RequestBody @Parameter(name = "update role") @Validated(GroupB.class) RoleVo roleVo) {
		Role role = roleService.updateByPrimaryKeySelective(roleVo);
		return role != null ? Result.success(role) : Result.error();
	}

	/**
	 * Query get role list
	 */
	@ApiIgnore
	@ApiOperation(value = "Role list", notes = "query role list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNumber", value = "pagenumber", paramType = "query", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "pagesize", paramType = "query", required = true, dataType = "String"),
	})
	@GetMapping()
	public Result<RoleDto> getList(@ApiIgnore @Validated(GroupD.class) RoleVo roleVo) {
		PageMethod.startPage(roleVo.getPageNumber(), roleVo.getPageSize());
		List<RoleDto> list = roleService.selectByVo(roleVo);
		PageInfo<RoleDto> pageInfo = new PageInfo<>(list);
		return Result.success(pageInfo);
	}

}
